﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using UdpWpfDemo3;
using FinalTestMachine.Motion.Product;
using FinalTestMachine.Motion.Products;
using FinalTestMachine.View;
using FinalTestMachine.Funs;
using FinalTestMachine.Motion.Process;

namespace FinalTestMachine.Connect
{
    /// <summary>
    /// 基础的站位通讯对象
    /// </summary>
    public abstract class SiteUdpConnectBase
    {
        /// <summary>
        /// 与测试的通讯对象
        /// </summary>
        public class CheckMTPCommand
        {
            public CheckMTPCommand(string command)
            {
                Command = command;
            }

            /// <summary>
            /// 命令
            /// </summary>
            public string Command { get; }

            private Action<UdpWpfDemo3.MTP> CheckAction;

            public void SetCheckAction(Action<UdpWpfDemo3.MTP> action)
            {
                CheckAction = action;
            }

            public void ExecuteCheckAction(UdpWpfDemo3.MTP mTP)
            {
                CheckAction?.Invoke(mTP);
            }
        }

        public SiteUdpConnectBase(IConnectDLL.IConnectT<UdpWpfDemo3.MTP> uDPHELPer, string name)
        {
            Connect = uDPHELPer;
            Name = name;
            Connect.RecevieTActionEventHander += Connect_RecevieTActionEventHander;

            checkMTPCommandsList.Add(CheckFinish);
            checkMTPCommandsList.Add(CheckLimit);
            checkMTPCommandsList.Add(CheckReload);
           
            RegisterCheckCommand();
        }

        public void AddCommand(CheckMTPCommand checkMTPCommand)
        {
            checkMTPCommandsList.Add(checkMTPCommand);
        }

        public string Name { get; }

        public readonly List<CheckMTPCommand> checkMTPCommandsList = new List<CheckMTPCommand>();

        /// <summary>
        /// 当前的测试项结果
        /// </summary>
        public ObservableCollection<ProductTestStationTestItem> TestItemList = new ObservableCollection<ProductTestStationTestItem>();

        /// <summary>
        /// 基础的通讯对象
        /// </summary>
        public IConnectDLL.IConnectT<UdpWpfDemo3.MTP> Connect { get; }

        /// <summary>
        /// 测试是否Ok
        /// </summary>
        public bool IsOk { get; private set; }

        /// <summary>
        /// 测试是否结束
        /// </summary>
        public bool IsFinished { get; private set; }

        private bool _islimit = false;

        public bool IsLimit
        {
            get { return _islimit; }
            set
            {
                if (value != _islimit)
                {
                    _islimit = value;
                    SendToLimit(_islimit);
                }
            }
        }

        private readonly object ob_Lock = new object();

        public Action ReLoadAction;

        #region 命令

        /// <summary>
        /// 测试是否结束
        /// </summary>
        private CheckMTPCommand CheckFinish = new CheckMTPCommand("FINISH");

        /// <summary>
        /// 权限
        /// </summary>
        private CheckMTPCommand CheckLimit = new CheckMTPCommand("LIMIT");

        /// <summary>
        /// 重新上顶
        /// </summary>
        private CheckMTPCommand CheckReload = new CheckMTPCommand("RELOAD");
        #endregion 命令

        private void RegisterCheckCommand()
        {
            CheckFinish.SetCheckAction(s =>
            {
                Process_AutoRun.Cur.WriteToUser($"{Name}开始响应：{CheckFinish.Command}");

                if (s.result.ToUpper() == "OK" || s.result.ToUpper() == "PASS")
                {
                    Process_AutoRun.Cur.WriteToUser("当前判断结果为OK");
                   IsOk = true;
                }
                else
                {
                    IsOk = false;
                }
                if (s.text != null)
                {
                    string[] tempArray = s.text.Split(';');

                    string strNew = "";
                    for (int i = 0; i < tempArray.Length; i++)
                    {
                        strNew += "[";
                        strNew += tempArray[i];
                        strNew += "]";
                    }
                    List<ProductTestStationTestItem> testItemMsgs = ProductTestStationTestItem.ChangeMsg(strNew);

                    foreach (var item in testItemMsgs)
                    {
                        TestItemList.Add(item);
                    }
                }

                IsFinished = true;
            });

            CheckLimit.SetCheckAction(s =>
            {
                WriteToUser($"{Name}开始响应：{CheckLimit.Command}");

                SendToLimit(IsLimit);
            });

            CheckReload.SetCheckAction(s =>
            {
                WriteToUser($"{Name}开始响应：{CheckReload.Command}");

                MTP mc2Test = new MTP();
                mc2Test.command = CheckReload.Command;

                ReLoadAction?.Invoke();

                mc2Test.result = "OK";
                Connect.Send(mc2Test);
                WriteToUser($"{Name}发送：{mc2Test.command}, {mc2Test.text}, {mc2Test.result}");
            });
        }

        private void Connect_RecevieTActionEventHander(MTP obj)
        {
            try
            {
                lock (ob_Lock)
                {
                    if (obj == null)
                        return;
                    Process_AutoRun.Cur.WriteToUser($"{Name}收到信息：{obj.command},{obj.text},{obj.result}");
                    foreach (var item in checkMTPCommandsList)
                    {
                        if (item.Command.ToUpper() == obj.command.ToUpper())
                        {
                            item.ExecuteCheckAction(obj);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Motion.StaticInitial.Motion.WriteErrToUser($"{Name} 通讯处理异常：" + ex.Message);
            }
        }

        protected static void WriteToUser(string str)
        {
            Motion.StaticInitial.Motion.WriteToUser(str);
        }

        protected static void writeErrToUser(string str)
        {
            Motion.StaticInitial.Motion.WriteErrToUser(str);
        }

        /// <summary>
        /// 发送换型
        /// </summary>
        public void SendToChangeType()
        {
            MTP mc2Test = new MTP();
            mc2Test.command = "CHANGEMODEL";
            mc2Test.text = "MODEL=" + WorkItemManagerHelper.LoadedName;
            mc2Test.result = "";
            Connect.Send(mc2Test);
            WriteToUser($"{Name}发送：{mc2Test.command}, {mc2Test.text}, {mc2Test.result}");
        }

        /// <summary>
        /// 发送开始
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="localStation"></param>
        /// <param name="socket"></param>
        public void SendToStart(string sn, string localStation, string socket)
        {
            MTP mc2Test = new MTP();
            mc2Test.command = "START";
            string text = "";
            mc2Test.text = $"SN={sn};PARA={localStation};SOCKET={socket}" + text;

            mc2Test.result = null;
            IsFinished = false;
            IsOk = false;
            TestItemList.Clear();
            Connect.Send(mc2Test);
            WriteToUser($"{Name}发送：{mc2Test.command}, {mc2Test.text}, {mc2Test.result}");
        }

        public void SendToNotTest(string sn, string localStation, string socket)
        {
            MTP mc2Test = new MTP();
            mc2Test.command = "NOTTEST";

            mc2Test.text = $"SN={sn};PARA={localStation};SOCKET={socket}";

            mc2Test.result = null;
            IsFinished = false;
            IsOk = false;
            TestItemList.Clear();

            Connect.Send(mc2Test);
            WriteToUser($"{Name}发送：{mc2Test.command}, {mc2Test.text}, {mc2Test.result}");
        }

        public void SendToLimit(bool bl)
        {
            MTP mc2Test = new MTP();
            mc2Test.command = "LIMIT";
            if (bl == true)
            {
                mc2Test.text = "true";
            }
            else
            {
                mc2Test.text = "false";
            }

            Connect.Send(mc2Test);
            WriteToUser($"{Name}发送：{mc2Test.command}, {mc2Test.text}, {mc2Test.result}");
        }
    }
}